<?php if (!defined('BASEPATH'))  exit('No direct script access allowed');
/**
 * @filesource department_grid_form.php
 */
if(0){?><script><?php }?>

Ext.define('Hydcrm.department.DepartmentGridForm', {
    extend: 'Ext.form.Panel',
    
    requires: [
        'Ext.grid.*',
        'Ext.form.*',
        'Ext.layout.container.Column'
    ],
    xtype: 'form-grid',
    id: 'department_tab',
    frame: true,
    title: '部门管理',
    closable: true,
    autoScroll: true,
    bodyPadding: 5,
    layout: 'column',

    buttons: [{
        text: '保存',
        iconCls: 'icon-save',
        handler: function() {
            this.up('form').getForm().submit({
                url: Hydcrm.CONF.CONN_URL,
                params:{
                    module: 'department',
                    action: 'save_department', 
                    id: ''       
                }, 
                method: 'POST',
                waitMsg: '正在更新',
                success: function(form, action) {
                    list_department_ds.load();
                    //Ext.Msg.alert('正确信息', action.result.feedback);
                },
                failure: function(form, action) {
                    Ext.Msg.alert('错误信息', action.result.feedback);
                },
                scope: this
            });
        }
    },{
        text: '新增',
        iconCls: 'icon-add',
        handler: function() {//this.up('form').getForm().submit();
            this.up('form').getForm().reset();
        }
    }],

    initComponent: function(){
        var dsDepartmentTeam = Ext.create('Ext.data.Store', {
            fields: [ 'id','text'],
            proxy: {
                type: 'ajax',
                url: Hydcrm.CONF.CONN_URL,
                extraParams: {
                    module: 'team',
                    action: 'get_team_select'
                },
                reader: {
                    type: 'json',
                    root: Hydcrm.CONF.JSON_READER_ROOT
                }
            },
            autoLoad: true
        });
        var cboDepartmentTeam = Ext.create('Ext.form.ComboBox', {
            fieldLabel: '所属团队',
            store: dsDepartmentTeam,
            id:'teamid',
            name: 'teamid',
            queryMode: 'local',
            displayField: 'text',
            valueField: 'id',
            editable: false,
            allowBlank: false,
            //labelStyle: 'color:red',
            msgTarget:'side',
            forceSelection: true,
            emptyText:'未选择',
        });
        Ext.apply(this, {
            width: '100%',
            fieldDefaults: {
                labelAlign: 'right',
                labelWidth: 100,
                columnWidth: 0.8,
                margin: 8,
                anchor: '48%',
                msgTarget: 'side'
            },

            items: [{
                columnWidth: 0.65,
                xtype: 'gridpanel',
                id: 'department_gridpanel',
                store: list_department_ds,
                height: '400',
                columns: [{
                    text: '部门ID',
                    hidden: true,
                    sortable: false,
                    dataIndex: 'id'
                },{
                    text: '团队ID',
                    hidden: true,
                    sortable: false,
                    dataIndex: 'teamid'
                },{
                    text: '部门名称',
                    flex: 1,
                    sortable: true,
                    dataIndex: 'name'
                }, {
                    text: '所属团队',
                    width: 260,
                    sortable: true,
                    dataIndex: 'team_name'
                }, {
                    text: '部门人数',
                    width: 100,
                    sortable: true,
                    dataIndex: 'numtotal'
                }, {
                    text: '部门主管',
                    width: 100,
                    sortable: true,
                    dataIndex: 'header'
                }],
                bbar: Ext.create('Ext.PagingToolbar', {
                    store: list_department_ds,
                    displayInfo: true,
                    displayMsg: '显示条目 {0} - {1} of {2}',
                    emptyMsg: "无信息可显示",
                    inputItemWidth: 35,
                    items:['-']
                }),
                listeners: {
                    scope: this,
                    selectionchange: this.onSelectionChange
                }
            }, {
                columnWidth: 0.35,
                margin: '0 0 0 10',
                xtype: 'fieldset',
                id: 'department_fieldset',
                title:'部门明细',
                layout: 'column',
                defaultType: 'textfield',
                items: [{
                    fieldLabel: '部门ID',
                    name: 'id',
                    xtype: 'hidden'
                },{
                    fieldLabel: '部门名称',
                    name: 'name',
                    allowBlank: false
                },cboDepartmentTeam,{
                    fieldLabel: '部门人数',
                    name: 'numtotal'
                },{
                    fieldLabel: '部门主管',
                    name: 'header'
                }]
            }]
        });
        this.callParent();
    },
    onSelectionChange: function(model, records) {
        var rec = records[0];
        if (rec) { //alert(rec.get('id'));
            this.getForm().loadRecord(rec);
        }
    }
});